package com.tedu.bill.demo1;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Author：毕龙
 * Date: 2024/5/22 20:19
 */
public class TestDemo1 {
    public static void main(String[] args) throws Exception {
        Callable<Integer> c1 = new Callable<>() {

            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 0; i <= 50; i++) {
                    sum += i;
                    System.out.println(Thread.currentThread().getName()+"\t"+sum);
                }
                return sum;
            }
        };
        Callable<Integer> c2 = new Callable<>() {

            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 51; i <= 100; i++) {
                    sum += i;
                    System.out.println(Thread.currentThread().getName()+"\t"+sum);
                }
                return sum;
            }
        };

        FutureTask<Integer> f1 = new FutureTask<>(c1);
        new Thread(f1).start();
        System.out.println(f1.get());
        FutureTask<Integer> f2 = new FutureTask<>(c2);
        new Thread(f2).start();
        System.out.println(f2.get());
        System.out.println(f1.get()+f2.get());
    }
}
